package org.wappumobileapp;

import java.util.Random;

import com.google.ads.*;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends BaseActivity implements OnClickListener {

	final Context context = this;

	private TextView lblTextNick;
	private EditText editNick;
	public static String nick = null;

	private AdView adView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.main);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.windowtitle);

		// AD
		// Look up the AdView as a resource and load a request.
		adView = (AdView) this.findViewById(R.id.adView);
		adView.loadAd(new AdRequest());

		lblTextNick = (TextView) findViewById(R.id.nick);

		if (nick == null || nick == "") {
			// custom dialog
			final Dialog dialog = new Dialog(context);
			dialog.setCancelable(false);
			dialog.setContentView(R.layout.nick_name);

			// set the custom dialog components - text, editText and button
			editNick = (EditText) dialog.findViewById(R.id.editNick);
			// Setting enter as "done"
			editNick.setImeOptions(EditorInfo.IME_ACTION_DONE);

			Button dialogButton = (Button) dialog.findViewById(R.id.buttonNick);
			// if button is clicked, close the custom dialog
			dialogButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					nick = editNick.getText().toString().replaceAll(" ", "");
					lblTextNick.setText(nick);
					dialog.dismiss();
				}
			});
			dialog.show();

		}

		lblTextNick.setText(nick);

		// Set up click listeners for all the buttons
		View getSugButton = findViewById(R.id.get_suggestion_button);
		getSugButton.setOnClickListener(this);
		View sendSugButton = findViewById(R.id.send_suggestion_button);
		sendSugButton.setOnClickListener(this);
		View historyButton = findViewById(R.id.history_button);
		historyButton.setOnClickListener(this);

		// background
		LinearLayout lv = (LinearLayout) findViewById(R.id.mainLayout);
		Bitmap background = BitmapFactory.decodeResource(getResources(),
				R.drawable.background);
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int height = metrics.heightPixels;
		int width = metrics.widthPixels;
		Random ran = new Random(); // generate random starting corner
		int w_start = ran.nextInt(background.getWidth() - width);
		int h_start = ran.nextInt(background.getHeight() - height);
		BitmapDrawable bg = new BitmapDrawable(
				getResources(),
				Bitmap.createBitmap(background, w_start, h_start, width, height));// new
		// BitmapDrawable(background);
		lv.setBackgroundDrawable(bg);

	}

	/** Called before the activity is destroyed. */
	@Override
	public void onDestroy() {
		// Destroy the AdView.
		if (adView != null) {
			adView.destroy();
		}

		super.onDestroy();
	}

	public void onClick(View v) {
		if (haveNetworkConnection()) {
			switch (v.getId()) {
			case R.id.get_suggestion_button:
				Intent i = new Intent(this, GetSuggestion.class);
				i.putExtra("Nick", nick);
				startActivity(i);
				break;
			case R.id.send_suggestion_button:
				Intent j = new Intent(this, SendSuggestion.class);
				j.putExtra("Nick", nick);
				startActivity(j);
				break;
			case R.id.history_button:
				Intent k = new Intent(this, History.class);
				k.putExtra("Nick", nick);
				startActivity(k);
				break;

			}
		} else {
			Toast.makeText(getApplicationContext(), R.string.NoConnection,
					Toast.LENGTH_LONG).show();

		}
	}

	@Override
	public void onBackPressed() {
		AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
		builder.setMessage("Do you really want to exit?")
				.setCancelable(false)
				.setPositiveButton("Quit",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								finish();
								GetFromService.clear();

							}
						})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
							}
						});
		AlertDialog alert = builder.create();
		alert.show();

	}

}
